/**
 * Created by Jack Xue on 20101030
 */
package org.code.jack.jweberp.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.code.jack.jweberp.domain.User;
import org.code.jack.jweberp.service.IUserService;
import org.springframework.stereotype.Controller;

@Controller("/login")
public class LoginAction extends BaseAction{
	
	@Resource
	private IUserService userServiceBean;
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("login");
	}
	
	public ActionForward checkLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		String randCode = request.getParameter("randCode").trim();
		String ip = request.getLocalAddr();			// IP
		if(randCode.equals(request.getSession().getAttribute("rand"))){
			User user = userServiceBean.login(userName, password, ip);
			if(user !=null){
				request.getSession().setAttribute("currentUser", user);
				return mapping.findForward("success");
			} else {
				request.setAttribute("msg", "用户名或密码有误");
			}
		} else {
			request.setAttribute("msg", "验证码有误");
		}
		return mapping.findForward("login");
	}

}
